using System;
using System.Collections.Generic;
using System.Text;
using Xeml.Document;


namespace XemlUtilities.Indexing
    {
        public sealed class PartitionIndexer
        {

            #region Singleton pattern
            static readonly PartitionIndexer instance = new PartitionIndexer();

            static PartitionIndexer()
            {
            }

            PartitionIndexer()
            {


            }

            public static PartitionIndexer Instance
            {
                get
                {
                    return instance;
                }
            }
            #endregion

            private StoryBase story;
            private int currentIndx = 0;

            private void Init(StoryBase sb)
            {
                if (this.story != sb)
                {
                    this.story = sb;

                    FindCurrentIndx();
                }
            }

            public int NextId(StoryBase sb)
            {
                Init(sb);
                lock (this)
                {
                    this.currentIndx++;
                }
                return this.currentIndx;
            }

            public void NormalizeIndex()
            {
                throw new NotImplementedException();
            }

            private void FindCurrentIndx()
            {
                this.currentIndx = 0;
                foreach (ObservationPoint op in story.ObservationPoints)
                {
                    foreach (Observation o in op.Observations)
                    {
                        foreach (Partition p in o.Partitions)
                        {
                            this.currentIndx = Math.Max(p.Id, this.currentIndx);
                        }
                    }
                }
            }

        }
  

}
